package com.example.demo.service.impl;

import com.example.demo.client.DeepSeekWebClient;
import com.example.demo.common.R;
import com.example.demo.entity.allContents.AllContents;
import com.example.demo.entity.allContents.AllContentsInsOne;
import com.example.demo.entity.allContents.AllContentsSelAll;
import com.example.demo.entity.deepseek.DeepSeekRequest;
import com.example.demo.entity.deepseek.DeepSeekResponse;
import com.example.demo.entity.sysConfig.SysConfig;
import com.example.demo.entity.sysConfig.SysConfigSelAll;
import com.example.demo.exception.BusinessException;
import com.example.demo.service.AiChatService;
import com.example.demo.service.AllContentsService;
import com.example.demo.service.SysConfigService;
import com.example.demo.service.baseContext.BaseContextService;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AlChatServiceImpl implements AiChatService {
    @Autowired
    private AllContentsService allContentsService;
    @Autowired
    private SysConfigService sysConfigService;
    @Autowired
    private BaseContextService bcs;

    // private DeepSeekWebClient deepSeekClient;
    private Map<String, DeepSeekWebClient> clients = new HashMap<>();

    // @PostConstruct
    public void init() {
        R<List<SysConfig>> listR = sysConfigService.selAllSysConfig(new SysConfigSelAll());
        // String deepseekKey = listR.getData().get(0).getDeepseekKey();
        // this.deepSeekClient = new DeepSeekWebClient(deepseekKey);
        if (listR.getData().size() > 0) {
            this.clients.put(
                    bcs.getUserData().getUserId(),
                    new DeepSeekWebClient(listR.getData().get(0).getDeepseekKey())
            );
        }
        else {
            throw new BusinessException("请先在设置中添加key");
        }
    }

    public String getAssistantReply(DeepSeekRequest.Message[] messages) {
        // DeepSeekResponse response = deepSeekClient.chatCompletion(messages).block();
        // return response.getChoices().get(0).getMessage().getContent();
        if (this.clients.get(bcs.getUserData().getUserId()) == null) {
            init();
        }
        DeepSeekResponse response = this.clients.get(bcs.getUserData().getUserId()).chatCompletion(messages).block();
        return response.getChoices().get(0).getMessage().getContent();
    }

    @Override
    public R<String> aiChat(Integer titleId) {
        AllContentsSelAll allContentsSelAll = new AllContentsSelAll();
        allContentsSelAll.setTitleId(titleId);
        R<List<AllContents>> listR = allContentsService.selAllAllContents(allContentsSelAll);
        List<AllContents> contents = listR.getData();
        DeepSeekRequest.Message[] array = new DeepSeekRequest.Message[contents.size()];
        for (int i = 0; i < contents.size(); i++) {
            DeepSeekRequest.Message message = new DeepSeekRequest.Message(
                    contents.get(i).getRole(),
                    contents.get(i).getContent()
            );
            array[i] = message;
        }


        String data = getAssistantReply(array);


        AllContentsInsOne allContentsInsOne = new AllContentsInsOne();
        allContentsInsOne.setTitleId(titleId);
        allContentsInsOne.setRole("system");
        allContentsInsOne.setContent(data);
        allContentsService.insAllContents(allContentsInsOne);
        return R.ok(data);
    }
}
